package kubernetes

import (
	"errors"
	"k8s-admin/global"
	"k8s-admin/service/kubernetes"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

/*创建StatefulSet*/
func CreateClusterStatefulSet(ctx *gin.Context) {
	params := new(struct {
		Namespace       string `form:"namespace" json:"namespace" binding:"required"`
		StatefulSetData string `form:"statefulsetdata" json:"statefulsetdata" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {

		err = kubernetes.NewK8sInterface().CreateClusterStatefulSetYaml(cid, params.Namespace, params.StatefulSetData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}

}

/*获取statefulset列表*/
func StatefulsetLists(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		//global.TPLogger.Error(err)
		utils.ReturnContext(ctx).Failed("失败", err)
	} else {
		list, err := kubernetes.NewK8sInterface().GetClusterStatefulSetList(cid, params.Namespace)
		if err != nil {
			global.TPLogger.Error(err)
			utils.ReturnContext(ctx).Failed("失败", errors.New("Statefulset数据查询失败").Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}
}

/*获取具体的statefulset资源详情*/
func GetClusterStatefulSet(ctx *gin.Context) {
	params := new(struct {
		Namespace       string `form:"namespace" binding:"required"`
		StatefulSetName string `form:"statefulset_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		//global.TPLogger.Error(err)
		utils.ReturnContext(ctx).Failed("失败", err)
	} else {

		list, err := kubernetes.NewK8sInterface().GetClusterStatefulSet(cid, params.Namespace, params.StatefulSetName)
		if err != nil {
			global.TPLogger.Error(err)
			utils.ReturnContext(ctx).Failed("失败", errors.New("Statefulset数据查询失败").Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}
}

/*删除Statefulset*/
func DeleteClusterStatefulSet(ctx *gin.Context) {
	params := new(struct {
		Namespace       string `form:"namespace" binding:"required"`
		StatefulSetName string `form:"statefulsetname" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		//global.TPLogger.Error(err)
		utils.ReturnContext(ctx).Failed("失败", err)
	} else {
		err := kubernetes.NewK8sInterface().DeleteClusterStatefulSet(cid, params.Namespace, params.StatefulSetName)
		if err != nil {
			global.TPLogger.Error(err)
			utils.ReturnContext(ctx).Failed("失败", errors.New("删除失败").Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}
}

func UpdateClusterStatefulSet(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
		//StatefulSetName string `form:"statefulsetname" binding:"required"`
		Context string `form:"context" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err)
	} else {

		err = kubernetes.NewK8sInterface().UpdateClusterStatefulSet(cid, params.Namespace, params.Context)
		if err != nil {
			global.TPLogger.Error(err)
			utils.ReturnContext(ctx).Failed("失败", errors.New("Statefulset更新失败").Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}
}
